package com.chen.service;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 练习cookie和seesion
 */
@WebServlet(name = "CookieServlet")
public class CookieServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求和响应的编码格式
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
       /* 处理请求*/
        //创建cookie
        Cookie c = new Cookie("key","value");
        //        设置coolie的保存时间 单位秒  默认保存时间是浏览器关闭前
        c.setMaxAge(3*24*3600);
        c.setPath("url");
        resp.addCookie(c);
        /*获取cookie*/
        Cookie[] cookies = req.getCookies();


        /*响应处理结果*/
    }

    /**
     * @program: JAVAEE
     * @description: 学习servlet
     * @author: peanut
     * @create: 2019-05-12 16:09
     */
    public static class Myservice extends HttpServlet {
        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

            String userName = req.getParameter("userName");
            String passWord = req.getParameter("passWord");
            System.out.println("用户名："+userName+",密码："+passWord);
            resp.setContentType("text/html;charset=UTF-8");
            if(userName.equals("转发")){
                //q请求转发
              /*  req.getRequestDispatcher("login").forward(req,resp);*/
                //重定向
                resp.sendRedirect("https://www.baidu.com/");
                return;
            }else{
                resp.getWriter().write("用户名："+userName+",密码："+passWord);
            }
        }
    }
}
